let maxLen = 600;

const convertBase64 = function(callback, res, compressed) {
	var files = !!res.tempFiles ? res.tempFiles : res;
	//console.log("convertBase64.files", files);
	Promise.all(
		files.map((item) => {
			return fileToBase64(item, compressed);
		})
	).then((data) => {
		//console.log("convertBase64.data", data);
		if (!!callback) callback(data);
	});
};

const getRate = function(width, height, compressed) {
	//return 1;
	//计算缩放比例
	var rate = 1;
	if (width >= height) {
		if (width > compressed) {
			rate = compressed / width;
		}
	} else {
		if (height > compressed) {
			rate = compressed / height;
		}
	};
	//console.log("压缩比例:", rate * 100, width, height, compressed);
	return rate;
};

const fileToBase64 = function(file, compressed) { //H5调用的
	var fileName = file.name;
	if (compressed === undefined || compressed === null || compressed === "") compressed = maxLen;
	if (compressed === 0) {
		return new Promise((resolve, reject) => {
			//选择文件
			var reader = new FileReader(); //读取客户端上的文件
			reader.onload = function(e) {
				var base64 = e.target.result;
				resolve({
					fileName: fileName,
					base64: base64
				});
			};
			reader.readAsDataURL(file);
		});
	}
	return new Promise((resolve, reject) => {
		var img = new Image();
		img.onload = function() {
			//生成比例
			// console.log(img.width)
			// console.log(img.height)
			var rate = getRate(img.width, img.height, compressed);
			img.width = img.width * rate;
			img.height = img.height * rate;
			//生成canvas
			var canvas = document.createElement("canvas");
			var ctx = canvas.getContext("2d");
			canvas.width = img.width;
			canvas.height = img.height;
			ctx.drawImage(img, 0, 0, img.width, img.height);
			var type = file.type == 'image/png' ? 'image/png' : 'image/jpeg';
			var base64 = canvas.toDataURL(type, 1);
			resolve({
				fileName: fileName,
				base64: base64
			});
		};
		var reader = new FileReader(); //读取客户端上的文件
		reader.onload = function(e) {
			var url = e.target
				.result; //读取到的文件内容.这个属性只在读取操作完成之后才有效,并且数据的格式取决于读取操作是由哪个方法发起的.所以必须使用reader.onload，
			img.src = url; //reader读取的文件内容是base64,利用这个url就能实现上传前预览图片
		};
		reader.readAsDataURL(file);
	});
};

const compressedBase64 = function(url, type, compressed) {
	if (compressed == undefined) compressed = maxLen;
	if (type == undefined) type = "image/jpeg"; //var type = file.type == 'image/png' ? 'image/png' : 'image/jpeg';
	return new Promise((resolve, reject) => {
		var img = new Image();
		img.onload = function() {
			//生成比例
			// console.log(img.width)
			// console.log(img.height)
			var rate = getRate(img.width, img.height, compressed);
			img.width = img.width * rate;
			img.height = img.height * rate;
			//生成canvas
			var canvas = document.createElement("canvas");
			var ctx = canvas.getContext("2d");
			canvas.width = img.width;
			canvas.height = img.height;
			ctx.drawImage(img, 0, 0, img.width, img.height);
			var base64 = canvas.toDataURL(type, 1);
			resolve({
				base64: base64,
				width: img.width,
				height: img.height
			});
		};
		img.src = url; //reader读取的文件内容是base64,利用这个url就能实现上传前预览图片
	});
}

function getFileNameTemp(item) {
	if (item == null || item == undefined) return '';
	let tempArray = item.split('.');
	let last = tempArray[tempArray.length - 1];
	if (isImg(item)) {
		return '图片文件.' + last;
	} else if (last == "doc" || last == "docx") {
		return 'word文档.' + last;
	} else if (last == "xls" || last == "xlsx") {
		return 'excel表格.' + last;
	} else if (last == "ppt" || last == "pptx") {
		return 'ppt幻灯片.' + last;
	} else if (last == "zip" || last == "7z" || last == "rar") {
		return '压缩文件.' + last;
	} else if (last == "pdf") {
		return 'pdf文档.' + last;
	} else if (last == "txt" || last == "json") {
		return '文本文件.' + last;
	} else if (last == "mp4") {
		return '视频文件.' + last;
	} else if (last == "mp3") {
		return '音频文件.' + last;
	}
	return '未知文件.' + last;
}

//1.txt       data:text/plain;base64,
//2.doc     data:application/msword;base64,
//3.docx   data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,
//4.xls      data:application/vnd.ms-excel;base64,
//5.xlsx    data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,
//6.pdf     data:application/pdf;base64,
//7.pptx   data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,
//8.ppt     data:application/vnd.ms-powerpoint;base64,
//9.zip 	  data:application/x-zip-compressed;base64,UEsDBBQAA
//10.7z	  data:application/octet-stream;base64,N3q //7z和rar是一样的
//11.json  data:application/json;base64,5Y+R6L6+55yB5Lu95omT566X5Y+R6YCB5Y+R6YCB
//"doc", "docx", "xls", "xlsx", "ppt", "pptx", "zip","7z", "rar", "pdf", "txt", "json"
//"jpg", "jpeg", "png", "bmp", "gif", "ico"
//base64文件的头部
function getBese64Begin(item) {
	if (item == null || item == undefined) return '';
	let tempArray = item.split('.');
	let last = tempArray[tempArray.length - 1];
	var start = "";
	if (isImg(item)) {
		var type = last == 'png' ? 'png' : 'jpeg'; //ico的类型是x-icon,所以都编程jpeg吧
		start = 'data:image/' + type;
	} else if (last == "txt") {
		start = 'data:text/plain';
	} else if (last == "doc") {
		start = 'data:application/msword';
	} else if (last == "docx") {
		start = 'data:application/vnd.openxmlformats-officedocument.wordprocessingml.document';
	} else if (last == "xls") {
		start = 'data:application/vnd.ms-excel';
	} else if (last == "xlsx") {
		start = 'data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
	} else if (last == "pdf") {
		start = 'data:application/pdf';
	} else if (last == "pptx") {
		start = 'data:application/vnd.openxmlformats-officedocument.presentationml.presentation';
	} else if (last == "ppt") {
		start = 'data:application/vnd.ms-powerpoint';
	} else if (last == "zip") {
		start = 'data:application/x-zip-compressed';
	} else if (last == "json") {
		start = 'data:application/json';
	} else if (last == "rar" || last == "7z") { //7z,rar
		start = 'data:application/octet-stream';
	} else {
		start = 'data:application/octet-stream';
	}
	let result = start + ';base64,'
	return result;
}

function getLast(item) {
	if (item == null || item == undefined) return '';
	let tempArray = item.split(';');
	let last = tempArray[0];
	var start = "";
	if (item.indexOf("data:image/png") != -1) {
		start = 'png';
	} else if (item.indexOf("data:image/") != -1) {
		start = 'jpeg';
	} else if (item.indexOf("data:text/plain") != -1) {
		start = 'txt';
	} else if (item.indexOf("data:application/msword") != -1) {
		start = 'doc';
	} else if (item.indexOf("data:application/vnd.openxmlformats-officedocument.wordprocessingml.document") != -1) {
		start = 'docx';
	} else if (item.indexOf("data:application/vnd.ms-excel") != -1) {
		start = 'xls';
	} else if (item.indexOf("data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") != -1) {
		start = 'xlsx';
	} else if (item.indexOf("data:application/pdf") != -1) {
		start = 'pdf';
	} else if (item.indexOf("data:application/vnd.openxmlformats-officedocument.presentationml.presentation") != -1) {
		start = 'pptx';
	} else if (item.indexOf("data:application/vnd.ms-powerpoint") != -1) {
		start = 'ppt';
	} else if (item.indexOf("data:application/x-zip-compressed") != -1) {
		start = 'zip';
	} else if (item.indexOf("data:application/json") != -1) {
		start = 'json';
	} else if (item.indexOf("data:application/octet-stream") != -1) { //7z,rar
		start = 'rar';
	} else {
		start = "未知";
	}
	let result = "." + start
	return result;
}

const isImg = function(path) { //文件后缀是否是图片
	var array = ["jpg", "jpeg", "png", "bmp", "gif", "ico"];
	for (var i = 0; i < array.length; i++) {
		var temp = array[i];
		if (path.lastIndexOf(temp) == (path.length - temp.length)) {
			return true;
		}
	}
	return false;
}

const isBase64 = function(base64) {
	if (base64 == null) return false;
	return base64.length > 255;
}

const getFileName = function(base64) {
	if (isBase64(base64)) {
		var last = getLast(base64);
		return getFileNameTemp(last);
	} else {
		return getFileNameTemp(base64);
	}
}

export default {
	convertBase64,
	compressedBase64,
	getFileName,
	isImg
}
